﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;

using VxiUtil;
using VxiConfiguration;
using VxiGame;
using System.Security.Cryptography;

namespace VxiServer
{
    class PolCode0x1f : PolCode
    {
        public override byte Code
        {
            get { return 0x1f; }
        }

        public override void Work(TcpClient client, string ipAddress, byte[] buffer)
        {
            byte[] charList = new byte[2500];
            byte[] uList = new byte[300];

            charList[0] = 0xe0;
            charList[1] = 0x08;

            charList[4] = 0x49;
            charList[5] = 0x58;
            charList[6] = 0x46;
            charList[7] = 0x46;

            charList[8] = 0x20;
            charList[28] = 0x10;

            ClientUser user = ClientManagement.GetClient(ipAddress);
            // Select char ID's

            byte[] packet2 = new byte[] {
			0xc4, 0x01, 0x00, 0x00, 0x49, 0x58, 0x46, 0x46, 0x20, 0x00, 0x00, 0x00, 0x2a, 0x72, 0x4a, 0x94, 
			0x4f, 0x60, 0x27, 0xc4, 0x45, 0x4b, 0x7d, 0xcf, 0x27, 0x8e, 0x6d, 0xcd, 0x03, 0x00, 0x00, 0x00, 
			0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x56, 0x61, 0x6c, 0x65, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x05, 0x00,
			0x07, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x02, 0x00, 0x10, 0x00, 0x20, 0x00, 0x30,
			0x00, 0x40, 0x00, 0x50, 0x00, 0x60, 0x00, 0x70, 0x00, 0x01, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00,
			0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb5, 0xfa, 0x01, 0x00,
			0x7e, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
			0x01, 0x01, 0x01, 0x01, 0x46, 0x6e, 0xcf, 0x09, 0xde, 0x17, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x0a, 0x52, 0x03, 0x00, 0x0e, 0x08, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00 };

            Buffer.BlockCopy(Encoding.ASCII.GetBytes(VxiSettings.Settings.ServerName), 0,
                packet2, 60, 16);

            for (int j = 0; j < 16; j++)
            {
                Buffer.BlockCopy(packet2, 32, charList, 32 + 140 * j, 140);

                byte[] contentid = new byte[3];
                Buffer.BlockCopy(charList, 32, contentid, 0, 3);
                contentid[2] += BitConverter.GetBytes(j)[0];
                Buffer.BlockCopy(contentid, 0, charList, 32 + 140 * j, 3);
                Buffer.BlockCopy(contentid, 0, uList, 16 * (j + 1), 3);
            }

            uList[0] = 0x03;

            int i = 0;

            foreach (int charId in new List<int>())
            {
                int index = 32 + i * 140;
                Character character = new Character();
                // load char data
                Buffer.BlockCopy(BitConverter.GetBytes(charId), 0, uList, 20 * (i + 1), 4);
                Buffer.BlockCopy(BitConverter.GetBytes(charId), 0, charList, index + 4, 4);
                Buffer.BlockCopy(Encoding.ASCII.GetBytes(character.Name), 0, charList, index + 12, 16);
                charList[46 + index] = BitConverter.GetBytes(character.MainJob.JobID)[0];
                charList[44 + index] = character.RaceID;
                charList[56 + index] = character.FaceID;
                charList[73 + index] = BitConverter.GetBytes(character.MainJob.JobID)[0];

                Buffer.BlockCopy(BitConverter.GetBytes(character.Equipment.Head), 0,
                    charList, 58 + index, 2);
                Buffer.BlockCopy(BitConverter.GetBytes(character.Equipment.Body), 0,
                    charList, 60 + index, 2);
                Buffer.BlockCopy(BitConverter.GetBytes(character.Equipment.Hands), 0,
                    charList, 62 + index, 2);
                Buffer.BlockCopy(BitConverter.GetBytes(character.Equipment.Legs), 0,
                    charList, 64 + index, 2);
                Buffer.BlockCopy(BitConverter.GetBytes(character.Equipment.Feet), 0,
                    charList, 66 + index, 2);
                Buffer.BlockCopy(BitConverter.GetBytes(character.Equipment.Main), 0,
                    charList, 68 + index, 2);
                Buffer.BlockCopy(BitConverter.GetBytes(character.Equipment.Sub), 0,
                    charList, 70 + index, 2);
                charList[72 + index] = character.CurrentZone.DisplayZoneID;
                i++;
            }

            uList[1] = 0x10;
            user.LobbySocket.Client.Send(uList.Take(272).ToArray());

            MD5 md5Hasher = MD5.Create();
            byte[] data = md5Hasher.ComputeHash(charList);

            Buffer.BlockCopy(data, 0, charList, 12, data.Length);
            client.Client.Send(charList.Take(2272).ToArray());
        }
    }
}
